//
//  BCStringEx.swift
//  NewXueBa
//
//  Created by Shine on 2019/6/3.
//  Copyright © 2019 jmxQAQ. All rights reserved.
//

import Foundation

extension String {
    
    enum TimeType: String {
        case TimeType_yMd_Hms = "yyyy-MM-dd HH:mm:ss" //年月日 时分秒
        case TimeType_yMd_Hm = "yyyy-MM-dd HH:mm" //年月日 时分
        case TimeType_yMd = "yyyy-MM-dd" //年月日
        case TimeType_Md_Hms = "MM-dd HH:mm:ss" //月日 时分秒
        case TimeType_Md = "MM月dd日"//月日
        case TimeType_Hms = "HH:mm:ss" //时分秒（24小时）
        case TimeType_Hm = "HH:mm" //时分（24小时）
    }
    
    /// MD5加密
    var md5: String {
        let md5Length = Int(CC_MD5_DIGEST_LENGTH)
        var result: [CUnsignedChar] = Array(repeating: 0, count: md5Length)
        let data = cString(using: .utf8)!
        CC_MD5(data, CC_LONG(data.count-1), &result)
        return (0..<md5Length).reduce("") { $0 + String(format: "%02hhx", result[$1])}
    }
    
    func trim() -> String {
        return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
    }
    
    /// 验证邮编号
    var isZipcode: Bool {
        let regex = "[1-9]\\d{5}(?!\\d)"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: self)
    }
   
    //MARK: -  正则表达式验证
    public enum ValidatedType: String {
        case Email = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
        case PhoneNumber = "^1(3[0-9]|4[56789]|5[0-9]|6[6]|7[0-9]|8[0-9]|9[89])\\d{8}$"
    }
    
    /// 时间戳转日期
    ///
    /// - Parameters:
    ///   - timeStamp: s时间戳
    ///   - format: 格式形式
    /// - Returns: 转出的时间字符串
    static func getDateFormatString(timeStamp: Double, format: TimeType) -> String {
        let interval = TimeInterval(timeStamp)
        let date = Date(timeIntervalSince1970: interval)
        let dateformatter = DateFormatter()
        //自定义日期格式
        dateformatter.dateFormat = format.rawValue
        return dateformatter.string(from: date)
    }
    
    /// 获取当前时间
    ///
    /// - Parameter format: 时间类型
    /// - Returns: 返回的
    static func getNowTimeString(format: TimeType) -> String {
        let date = Date()
        let dateformatter = DateFormatter()
        //自定义日期格式
        dateformatter.dateFormat = format.rawValue
        return dateformatter.string(from: date)
    }
    /**
     正则验证
     - parameter type: 验证类型
     - returns:验证结果
     */
    func validateText(_ validatedType: ValidatedType) -> Bool {
        do {
            let pattern = validatedType.rawValue
            let regex: NSRegularExpression = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
            let matches = regex.matches(in: self, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, self.count))
            return matches.count > 0
        }
        catch {
            return false
        }
    }
    
    /**
     根据时长格式化输出
     - parameter duration: 单位(秒)
     
     - returns: 返回格式化后的时长
     */
   static func getFormatterDuration(_ duration: Int) -> String {
        let hour = duration / 3600
        let minute = (duration - hour * 3600) / 60
        if hour <= 0 {
            return "\(minute)分钟"
        }
        if minute <= 0 {
            return "\(hour)小时"
        }
        return "\(hour)小时\(minute)分钟"
    }
    
    
    /// 获取本地化字符串
    ///
    /// - Returns: 本地化字符串
    func localized() -> String {
        return NSLocalizedString(self, comment: self)
    }
    
}
